package com.cn.zpark.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * @PackageName: com.cn.zpark.utils
 * @ClassName: JDBCUtils
 * @Description:JDBC工具类
 * @author: RZS
 * @date: 2022/3/30  19:35
 */
public class JDBCUtils {
    // 准备数据库连接信息
    private static String url = "jdbc:mysql://192.168.83.128:3306/java_web02?useSSL=false&serverTimezone=UTC&characterEncoding=UTF-8";
    private static String user = "root";
    private static String pwd = "root";
    private static String driver = "com.mysql.jdbc.Driver";

    /**
     * 获取数据库连接对象
     * @return
     */
    public static Connection getConn() throws Exception {
        // 注册驱动
        Class.forName(driver);
        // 返回数据库连接对象
        return DriverManager.getConnection(url, user, pwd);
    }

    /**
     * 关闭资源
     * @param conn
     * @param stat
     */
    public static void close(Connection conn, Statement stat){
        try {
            if(stat != null){
                stat.close();
            }

            if(conn != null){
                conn.close();
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}
